<?php
// @formatter:off
/**
 * @file ct.previews.update.php
 * @author Alejandro Dario Simi
 * @date $Date: 2013-06-05 00:31:35 +0000 (Wed, 05 Jun 2013) $
 *
 * $Id: ct.previews.update.php 69 2013-06-05 00:31:35Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/tags/wcomix-1.0-BETA1/cron/ct.previews.update.php $
 */
// @formatter:on

class WCComicsAutopreviews extends WCCronTool {
	public function run($spacer = "") {
		global $wcGroupsHolder;
		global $wcTagsHolder;
		global $wcDirectoriesHolder;
		global $wcCollectionsHolder;
		global $wcDefaults;
		global $wcDatabaseAccess;

		$db = WCConnection::Instance();
		$dbCode = md5(__FILE__);
		$dbPrefix = $wcDatabaseAccess['prefix'];

		$done = false;
		$doneCount = 1;
		foreach($wcGroupsHolder->groups() as $group) {
			if(is_readable($group->root())) {
				echo "{$spacer}Checking group '{$group->name}' ({$group->id}):\n";

				foreach($group->directoriesFull() as $directory) {
					foreach($directory->membersFull() as $comic) {
						$incrementDone = false;

						if(!$comic->generated) {
							$incrementDone = true;

							echo "{$spacer}\tChecking '{$comic->name}' ({$comic->id}@{$comic->directory}):";
							if($comic->expand()) {
								echo "\n";
								$ok = true;

								if(!is_readable($comic->previewPath())) {
									echo "{$spacer}\t\tGenerating gallery preview: ";
									$comic->generateThumb();
									if(is_readable($comic->previewPath())) {
										echo WCColorText::ShellGreen("Done")."\n";
									} else {
										echo WCColorText::ShellRed("Failed")."\n";
										$ok = false;
									}
								}
								$imagesCount = count($comic->images());
								for($i = 0, $j = 1; $i < $imagesCount; $i++, $j++) {
									$path = $comic->previewPathFor($i);
									if(!is_readable($path)) {
										echo "{$spacer}\t\tGenerating preview for {$j}/{$imagesCount}: ";
										$comic->previewUriFor($i, true);
										if(is_readable($path)) {
											echo WCColorText::ShellGreen("Done")."\n";
										} else {
											echo WCColorText::ShellRed("Failed")."\n";
											$ok = false;
										}
									}
								}

								recursive_unlink($comic->expandedPath());

								if($ok) {
									$comic->generated = true;
								}
							} else {
								echo " ".WCColorText::ShellRed("Failed").": Unable to expand comic\n";
							}
						}

						$doneCount += $incrementDone ? 1 : 0;

						$done = $wcDefaults['max-autoexpand'] < $doneCount;

						if($done) {
							break;
						}
					}

					if($done) {
						break;
					}
				}

				if($done) {
					break;
				}

				foreach($wcDirectoriesHolder->items($group) as $directory) {
					if($directory->count > 0 && !$directory->valid_preview) {
						echo "{$spacer}\tGenerating previews for directory '{$directory->name}' ({$directory->id}): ";
						$directory->generatePreviews();
						if($directory->valid_preview) {
							echo WCColorText::ShellGreen("Done")."\n";
						} else {
							echo WCColorText::ShellRed("Failed")."\n";
						}
					}
				}

				foreach($wcTagsHolder->items($group) as $tag) {
					if($tag->count > 0 && !$tag->valid_preview) {
						echo "{$spacer}\tGenerating previews for tag '{$tag->name}' ({$tag->id}): ";
						$tag->generatePreviews();
						if($tag->valid_preview) {
							echo WCColorText::ShellGreen("Done")."\n";
						} else {
							echo WCColorText::ShellRed("Failed")."\n";
						}
					}
				}

				foreach($wcCollectionsHolder->items($group) as $collection) {
					if(!$collection->valid_preview) {
						echo "{$spacer}\tGenerating previews for collection '{$collection->name}' ({$collection->id}): ";
						$collection->generatePreviews();
						if($collection->valid_preview) {
							echo WCColorText::ShellGreen("Done")."\n";
						} else {
							echo WCColorText::ShellRed("Failed")."\n";
						}
					}
				}
			}
		}
	}
	public function showHelp($spacer = "") {
		echo "{$spacer}This tool seeks for any comic that wasn't marked as it has all its previews generated.\n";
		echo "{$spacer}If a comic with not all previews generated is found it attempts to do so.\n\n";
		echo "{$spacer}It requires no extra parameters.\n";
	}
	protected function loadOptions() {
		$this->_optionsLoaded = true;
	}
}
?>